// 密码显示/隐藏切换
const togglePassword = document.getElementById('toggle-password');
const passwordInput = document.getElementById('password');
const passwordIcon = document.getElementById('password-icon');

togglePassword.addEventListener('click', function () {
    const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
    passwordInput.setAttribute('type', type);
    passwordIcon.className = type === 'password' ? 'fa fa-eye' : 'fa fa-eye-slash';
});

// 表单提交处理
const loginForm = document.getElementById('login-form');
const loading = document.getElementById('loading');

loginForm.addEventListener('submit', function (e) {
    e.preventDefault();

    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const remember = document.getElementById('remember').checked;

    // 显示加载动画
    loading.classList.remove('hidden');
    loading.classList.add('flex');

    // 模拟登录请求（可替换为真实 API）
    setTimeout(() => {
        // 隐藏加载动画
        loading.classList.add('hidden');
        loading.classList.remove('flex');

        // 简单验证
        if (username && password) {
            alert('登录成功！');
            // 可跳转：window.location.href = '/dashboard';
        } else {
            alert('请输入用户名和密码！');
        }
    }, 1500);
});

// 输入框焦点效果
const inputs = document.querySelectorAll('input[type="text"], input[type="password"]');
inputs.forEach(input => {
    input.addEventListener('focus', function () {
        this.parentElement.querySelector('i').classList.remove('text-gray-dark/50');
        this.parentElement.querySelector('i').classList.add('text-primary');
    });

    input.addEventListener('blur', function () {
        if (!this.value) {
            this.parentElement.querySelector('i').classList.remove('text-primary');
            this.parentElement.querySelector('i').classList.add('text-gray-dark/50');
        }
    });
});

// 回车键登录
document.addEventListener('keypress', function (e) {
    if (e.key === 'Enter') {
        loginForm.dispatchEvent(new Event('submit'));
    }
});